package com.tencent.cloud.uikit.ui;

import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.githang.statusbar.StatusBarCompat;
import com.tencent.cloud.iov.util.ActivityUtils;
import com.tencent.cloud.iov.util.ResourcesUtils;
import com.tencent.cloud.iov.util.storage.TXSharedPreferencesUtils;
import com.tencent.cloud.uikit.R;
import com.tencent.cloud.uikit.ui.widget.FitsWindowsFrameLayout;
import com.tencent.cloud.uikit.utils.SystemUiUtils;

import java.util.Locale;

import static com.tencent.cloud.iov.util.ResourcesUtils.getResources;

/**
 * @author showxiao
 * @date 2018/8/3
 */
public class BaseIovActivity extends AppCompatActivity {

    private FitsWindowsFrameLayout mFitsWindowsFrameLayout;
    String language;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 模态窗口
        if (isModalType()) {
            overridePendingTransition(R.anim.slide_in_up, R.anim.no_anim);
        }

        // 透明状态栏
        SystemUiUtils.translucent(this);

        // 窗口背景
        getWindow().setBackgroundDrawable(
                new ColorDrawable(ResourcesUtils.getColor(R.color.bg_color_primary))
        );

        // 预先添加系统窗口边距适配布局，以确保正确获取系统窗口边距
        if (fitsSystemWindows()) {
            prepareFitsWindowsFrameLayout();
        }

        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.white_color));

        language = TXSharedPreferencesUtils.getValue("app_language");
        if(!TextUtils.isEmpty(language)){
            if(language.equals("中文")){
                TXSharedPreferencesUtils.setValue("app_language", "中文");
                changeAppLanguage(Locale.SIMPLIFIED_CHINESE);
            }else if(language.equals("English")){
                TXSharedPreferencesUtils.setValue("app_language", "English");
                changeAppLanguage(Locale.US);
            }
        }
    }

    /**
     * 更改应用语言
     *
     * @param locale
     */
    public void changeAppLanguage(Locale locale) {
        DisplayMetrics metrics = getResources().getDisplayMetrics();
        Configuration configuration = getResources().getConfiguration();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            configuration.setLocale(locale);
        } else {
            configuration.locale = locale;
        }
        getResources().updateConfiguration(configuration, metrics);
    }

    /**
     * 是否自适应系统窗口边距（状态栏、标题栏和导航栏）
     *
     * @return  true 表示自动适应系统窗口边距，内容区域非满屏显示
     *          false 表示不自适应系统窗口边距，内容区域满屏显示
     */
    protected boolean fitsSystemWindows() {
        return true;
    }

    /**
     * 标题栏是否覆盖内容布局（与内容布局重叠）
     *
     * 仅在 {@link #fitsSystemWindows()} 为 true 时有效
     *
     * @return
     */
    protected boolean isActionBarOverlay() {
        return false;
    }

    /**
     * 状态栏是否覆盖内容布局（与内容布局重叠）
     *
     * 仅在 {@link #fitsSystemWindows()} 为 true 时有效
     *
     * @return
     */
    protected boolean isStatusBarOverlay() {
        return false;
    }

    /**
     * 是否模态页面（参考 iOS 概念）
     *
     * 模态页面切换动画为上下平移
     */
    protected boolean isModalType() {
        return false;
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        this.setContentView(LayoutInflater.from(this).inflate(layoutResID, null));
    }

    @Override
    public void setContentView(View view) {
        if (fitsSystemWindows() && mFitsWindowsFrameLayout != view) {
            prepareFitsWindowsFrameLayout();
            mFitsWindowsFrameLayout.removeAllViews();
            mFitsWindowsFrameLayout.addView(view);
        } else {
            super.setContentView(view);
        }
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        if (fitsSystemWindows() && mFitsWindowsFrameLayout != view) {
            prepareFitsWindowsFrameLayout();
            mFitsWindowsFrameLayout.removeAllViews();
            mFitsWindowsFrameLayout.addView(view, params);
        } else {
            super.setContentView(view, params);
        }
    }

    private void prepareFitsWindowsFrameLayout() {
        if (mFitsWindowsFrameLayout == null) {
            mFitsWindowsFrameLayout = new FitsWindowsFrameLayout(this);
            mFitsWindowsFrameLayout.setActionBarOverlay(isActionBarOverlay());
            mFitsWindowsFrameLayout.setStatusBarOverlay(isStatusBarOverlay());
            setContentView(mFitsWindowsFrameLayout);
        }
    }

    @Override
    public void finish() {
        super.finish();
        if (isModalType()) {
            overridePendingTransition(0, R.anim.slide_out_down);
        }
    }
}
